/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package cn.apestar.modules.specification.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.apestar.common.config.Global;
import cn.apestar.common.persistence.Page;
import cn.apestar.common.web.BaseController;
import cn.apestar.common.utils.StringUtils;
import cn.apestar.modules.specification.entity.ApestarSpecification;
import cn.apestar.modules.specification.service.ApestarSpecificationService;

/**
 * 规格管理Controller
 * @author yudw
 * @version 2017-04-05
 */
@Controller
@RequestMapping(value = "${adminPath}/specification/apestarSpecification")
public class ApestarSpecificationController extends BaseController {

	@Autowired
	private ApestarSpecificationService apestarSpecificationService;
	
	@ModelAttribute
	public ApestarSpecification get(@RequestParam(required=false) String id) {
		ApestarSpecification entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = apestarSpecificationService.get(id);
		}
		if (entity == null){
			entity = new ApestarSpecification();
		}
		return entity;
	}
	
	@RequiresPermissions("specification:apestarSpecification:view")
	@RequestMapping(value = {"list", ""})
	public String list(ApestarSpecification apestarSpecification, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ApestarSpecification> page = apestarSpecificationService.findPage(new Page<ApestarSpecification>(request, response), apestarSpecification); 
		model.addAttribute("page", page);
		return "common/specification/apestarSpecificationList";
	}

	@RequiresPermissions("specification:apestarSpecification:view")
	@RequestMapping(value = "form")
	public String form(ApestarSpecification apestarSpecification, Model model) {
		model.addAttribute("apestarSpecification", apestarSpecification);
		return "common/specification/apestarSpecificationForm";
	}

	@RequiresPermissions("specification:apestarSpecification:edit")
	@RequestMapping(value = "save")
	public String save(ApestarSpecification apestarSpecification, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, apestarSpecification)){
			return form(apestarSpecification, model);
		}
		apestarSpecificationService.save(apestarSpecification);
		addMessage(redirectAttributes, "保存规格管理成功");
		return "redirect:"+Global.getAdminPath()+"/specification/apestarSpecification/?repage";
	}
	
	@RequiresPermissions("specification:apestarSpecification:edit")
	@RequestMapping(value = "delete")
	public String delete(ApestarSpecification apestarSpecification, RedirectAttributes redirectAttributes) {
		apestarSpecificationService.delete(apestarSpecification);
		addMessage(redirectAttributes, "删除规格管理成功");
		return "redirect:"+Global.getAdminPath()+"/specification/apestarSpecification/?repage";
	}

}